package com.shan.mybatis.controllor;

import com.shan.mybatis.pojo.Emp;
import com.shan.mybatis.pojo.Result;
import com.shan.mybatis.service.EmpService;
import com.shan.mybatis.utils.JwtUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author: ShanYuqing
 * @Version: v1.0
 */
@Slf4j
@RestController
public class LoginControllor {
    @Autowired
    private EmpService service;

    @PostMapping("/login")
    public Result login(@RequestBody Emp emp) {
        Emp e = service.login(emp);
        if(e!=null){
            Map<String,Object> claims = new HashMap<>();
            claims.put("id",e.getId());
            claims.put("name",e.getName());
            claims.put("username",e.getUsername());
             String s = JwtUtils.generateJwt(claims);
             return Result.success(s);
        }
        return Result.error("用户名或密码错误");
    }

}
